﻿using NLog;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsWeChartDemo.Models;

namespace WindowsFormsWeChartDemo.Common
{
    public class SendMessageJob : IJob
    {
        private WeChartHelper weChartHelper = new WeChartHelper();
        private Logger logger = LogManager.GetCurrentClassLogger();

        public delegate void WriteLogHandler(string log);
        public static WriteLogHandler WriteLogEvent;

        public void Execute(IJobExecutionContext context)
        {
            Task.Run(async () =>
            {
                weChartHelper.OtherEvent += OtherEvent;
                weChartHelper.SendCompletedEvent += SendCompletedEvent;
                await weChartHelper.SendMessageAsync();
            });
        }
        /// <summary>
        /// 发送完成后的业务逻辑
        /// </summary>
        /// <param name="model"></param>
        private void SendCompletedEvent(SendMessageModel model)
        {
            string msg = $"消息发送完成：AppID：{model.appid},OpenId:{model.openid},Message:{model.message}";
            Write(msg);
            Write($"发送结果：{model.Result}");
        }

        private void OtherEvent(string msg)
        {
            Write(msg);
        }

        private void Write(string log)
        {
            logger.Info(log);
            WriteLogEvent?.Invoke(log);
        }
    }
}
